﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.OptimizationModule
{
    public class FormatDataSource 
    {
        // 上下文
        DataContext _dataContext;

        /// <summary>
        /// 配置信息
        /// </summary>
        public OptimizeConfig OptimizConfig { get; set; }

        /// <summary>
        /// 案由要素优选配置
        /// </summary>
        public OptimizeElementConfig OptimizeElement { get; set; }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="dataContext"></param>
        public FormatDataSource(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        /// <summary>
        /// 格式化数据源
        /// </summary>
        public void FormatData(List<DataModel> dataList)
        {
            if (_dataContext.DataLayerOpt.SourdatModleList.Count > 0)
            {
                #region 对现有分析节点中重复节点进行去重
                if (dataList.FirstOrDefault(a => a.sourceData.sourceList.FirstOrDefault(b => b.AnalysisStatus == "0" && b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower()) != null) != null)
                {
                    List<DataModel> dataModelList = dataList.Where(a => a.sourceData.sourceList.FirstOrDefault(b => b.AnalysisStatus == "0" && b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower() && b.SourceCode.ToLower() != "mscourtrecord" && b.SourceCode.ToLower() != "xscourtrecord") != null).ToList();

                    for (int i = 0; i < dataModelList.Count; i++)
                    {

                        DataModel dataModel = dataModelList[i];
                        if (dataModel.XmlPath.IndexOf("案由要素") == -1)
                        {
                            for (int j = 0; j < dataModel.AttributesList.Count; j++)
                            {
                                AttributeInfo itemModel = dataModel.AttributesList[j];
                                Source sourceMolde = itemModel.sourceList.FirstOrDefault(a => a.AnalysisStatus == "0" && a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());
                                if (sourceMolde != null)
                                {
                                    //删除对应的数据来源
                                    itemModel.sourceList.Remove(sourceMolde);
                                    _dataContext.InteractiveShow.LogShow("删除数据节点:" + dataModel.XmlPath + ",删除属性:" + itemModel.Name + ",删除属性来源：" + JsonConvert.SerializeObject(sourceMolde));
                                }
                                if (itemModel.sourceList.Count == 0)
                                {
                                    dataModel.AttributesList.Remove(itemModel);
                                    _dataContext.InteractiveShow.LogShow("删除数据节点:" + dataModel.XmlPath + ",删除属性:" + itemModel.Name);
                                }
                                if (itemModel.sourceList.Count == 1)
                                {
                                    itemModel.sourceList[0].IsCheck = "1";
                                    itemModel.sourceList[0].AnalysisStatus = "1";
                                }
                                if (itemModel.sourceList.Count > 1)
                                {
                                    Optimization(itemModel.sourceList, dataModel);
                                }
                            }
                            Source sourceNode = dataModel.sourceData.sourceList.FirstOrDefault(a => a.AnalysisStatus == "0" && a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());
                            if (sourceNode != null)
                            {
                                dataModel.sourceData.sourceList.Remove(sourceNode);
                                _dataContext.InteractiveShow.LogShow("删除数据节点:" + dataModel.XmlPath + ",删除节点来源：" + JsonConvert.SerializeObject(sourceNode));
                            }
                            if (dataModel.sourceData.sourceList.Count == 0)
                            {
                                dataList.Remove(dataModel);
                                _dataContext.InteractiveShow.LogShow("删除数据节点:" + dataModel.XmlPath + ",删除节点：" + JsonConvert.SerializeObject(dataModel));
                            }
                            if (dataModel.sourceData.sourceList.Count > 1)
                            {
                                Optimization(dataModel.sourceData.sourceList, dataModel);
                            }
                        }

                    }
                }
                #endregion

                #region 对现有优选节点进行保存
                FromatNode(dataList);
                #endregion
            }
        }


        /// <summary>
        /// 节点保存
        /// </summary>
        /// <param name="dataList">需要处理的数据源</param>
        public void FromatNode(List<DataModel> dataList)
        {
            if (_dataContext.DataLayerOpt.SourdatModleList.Count > 0)
            {
                if (dataList.FirstOrDefault(a => a.sourceData.sourceList.FirstOrDefault(b => b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower() && b.AnalysisStatus == "1") != null) != null)
                {
                    List<DataModel> datamodeNodeList = dataList.Where(a => a.sourceData.sourceList.FirstOrDefault(b => b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower() && b.AnalysisStatus == "1") != null).ToList();
                    foreach (DataModel dataModelNode in datamodeNodeList)
                    {
                        foreach (AttributeInfo attributemodel in dataModelNode.AttributesList)
                        {
                            Source attrSource = attributemodel.sourceList.FirstOrDefault(a => a.AnalysisStatus == "1" && a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());
                            if (attrSource != null)
                            {
                                attrSource.AnalysisStatus = "0";
                            }
                        }
                        Source nodeSoure = dataModelNode.sourceData.sourceList.FirstOrDefault(a => a.AnalysisStatus == "1" && a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());

                        if (nodeSoure != null)
                        {
                            nodeSoure.AnalysisStatus = "0";
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 接口属性转换
        /// </summary>
        /// <param name="dataList">需要处理的数据源</param>
        public void FromatNodeList(List<DataModel> dataList)
        {
            if (dataList.Count > 0)
            {
                if (dataList.FirstOrDefault(a => a.sourceData.sourceList.FirstOrDefault(b => b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower()) != null) != null)
                {
                    List<DataModel> datamodeNodeList = dataList.Where(a => a.sourceData.sourceList.FirstOrDefault(b => b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower()) != null).ToList();
                    foreach (DataModel dataModelNode in datamodeNodeList)
                    {
                        foreach (AttributeInfo attributemodel in dataModelNode.AttributesList)
                        {
                            Source attrSource = attributemodel.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());
                            if (attrSource != null)
                            {
                                attrSource.AnalysisStatus = "0";
                            }
                        }
                        Source nodeSoure = dataModelNode.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());

                        if (nodeSoure != null)
                        {
                            nodeSoure.AnalysisStatus = "0";
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 转换分析结果
        /// </summary>
        /// <param name="dataList"></param>
        public void FromatNodeListAsiss(List<DataModel> dataList)
        {
            if (dataList.Count > 0)
            {
                if (dataList.FirstOrDefault(a => a.sourceData.sourceList.FirstOrDefault(b => b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower()) != null) != null)
                {
                    List<DataModel> datamodeNodeList = dataList.Where(a => a.sourceData.sourceList.FirstOrDefault(b => b.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower()) != null).ToList();
                    foreach (DataModel dataModelNode in datamodeNodeList)
                    {
                        foreach (AttributeInfo attributemodel in dataModelNode.AttributesList)
                        {
                            Source attrSource = attributemodel.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());
                            if (attrSource != null)
                            {
                                attrSource.AnalysisStatus = "1";
                            }
                        }
                        Source nodeSoure = dataModelNode.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == _dataContext.DataLayerAns.SourceCode.ToLower());

                        if (nodeSoure != null)
                        {
                            nodeSoure.AnalysisStatus = "1";
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 优选人员
        /// </summary>
        /// <param name="sourceList">需要优选的数据集合</param>
        public void Optimization(List<Source> sourceList, DataModel dataModelRoot)
        {
            string opsoursname = "";
            foreach (Source source in sourceList)
            {
                if (!string.IsNullOrEmpty(opsoursname))
                {
                    List<Sourcedata> sourceDatade = OptimizConfig.Sourcedatadefine[0].SourcedataList;
                    Sourcedata sourcedataop = sourceDatade.FirstOrDefault(a => a.Code.ToLower() == opsoursname.ToLower());
                    Sourcedata sourcedataas = sourceDatade.FirstOrDefault(a => a.Code.ToLower() == source.SourceCode.ToLower());
                    if (sourcedataop != null && sourcedataas != null)
                    {
                        Xmllable xmllable = OptimizConfig.XmllableList.FirstOrDefault(a => a.Xmlpath == dataModelRoot.XmlPath);
                        int opsum = ListByCount(xmllable.Priority.Split('>').ToList(), sourcedataop.id.ToString());
                        int assum = ListByCount(xmllable.Priority.Split('>').ToList(), sourcedataas.id.ToString());
                        if (assum < opsum)
                        {
                            opsoursname = source.SourceCode.ToLower();
                        }
                    }
                }
                else
                {
                    opsoursname = source.SourceCode;
                }
            }
        }

        /// <summary>
        /// 计算当前元素在List中的第几位
        /// </summary>
        /// <param name="list">集合</param>
        /// <param name="name">值</param>
        /// <returns></returns>
        public int ListByCount(List<string> list, string name)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (name.ToLower() == list[i])
                {
                    return i + 1;
                }
            }
            return 999;
        }
    }

}
